var modalTemplate = '<div class="modal fade" id="modalContent" >' +
        '<div class="modal-dialog modal-sm">' +
        '<div class="modal-content">' +
        '<div class="modal-header">' +
        '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
        '<h4 class="modal-title">Modal title</h4>' +
        '</div>' +
        '<div class="modal-body">' +
        '<p>One fine body&hellip;</p>' +
        '</div>' +
        '<div class="modal-footer">' +
        '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>' +
        '<button type="button" class="btn btn-primary">确定</button>' +
        '</div>' +
        '</div><!-- /.modal-content -->' +
        '</div><!-- /.modal-dialog -->' +
        '</div><!-- /.modal -->';

var lang = {
    "processing": "载入中", //处理页面数据的时候的显示,
    'search':'查询',
    lengthMenu: "每页 _MENU_ 条记录",
    paginate: {//分页的样式文本内容。
        previous: "上一页",
        next: "下一页",
        first: "第一页",
        last: "最后一页"
    },
    zeroRecords: "没有内容", //table tbody内容为空时，tbody的内容。
    //下面三者构成了总体的左下角的内容。
    info: "总_MAX_条记录，共_PAGES_ 页，显示第_START_ 到第 _END_条记录", //左下角的信息显示，大写的词为关键字。
    infoEmpty: "0条记录", //筛选为空时左下角的显示。
    infoFiltered: ""//筛选之后的左下角筛选提示(另一个是分页信息显示，在上面的info中已经设置，所以可以不显示)，
}
jQuery.messager = {
    alert: function (title, msg, type, style) {
        if ($("#modalContent").length > 0) {
            $("#modalContent").remove();
        }
        $("body").append(modalTemplate);
        var modalContent = $("#modalContent");
        if (type === "error") {
            $(".modal-title", modalContent).html(title);
            $(".modal-body", modalContent).html(msg);
            $(".btn-primary", modalContent).hide();
            modalContent.addClass('modal-danger');
        } else if (type === "info") {
            modalContent.addClass('modal-info');
            $(".modal-footer button", modalContent).addClass('btn-outline');
            $(".modal-title", modalContent).html(title);
            $(".modal-body", modalContent).html(msg);
            $(".btn-primary", modalContent).hide();
        }
        if (style) {
            modalContent.addClass(style);
        }
        modalContent.modal('show');
    },
    msg:function(msg){
        this.alert("提示信息",msg,'info');
    },
    error:function(msg){
        this.alert("出错了",msg,'error');
    },
    collapse: function (obj, msg) {
        $(".well", obj).html(msg);
        obj.collapse();
        setTimeout(function () {
            obj.collapse('hide');
        }, 3000);
    },
    confirm: function (title, msg, callback) {
        if ($("#modalContent").length > 0) {
            $("#modalContent").remove();
        }
        $("body").append(modalTemplate);
        var modalContent = $("#modalContent");
        modalContent.addClass('modal-warning');
        $(".modal-footer button", modalContent).addClass('btn-outline');
        $(".modal-title", modalContent).html(title);
        $(".modal-body", modalContent).html(msg);
        $(".btn-default", modalContent).html("取消");
        $(".btn-default", modalContent).addClass('pull-left');
        $('.modal').modal('show');
        $(".btn-primary", modalContent).on("click", function () {
            modalContent.modal('hide');
            if ($.isFunction(callback)) {
                callback(true);
            }
        });

    },
    progress: function (msg) {
        if ($("#modalContent").length > 0) {
            $("#modalContent").remove();
        }
        $("body").append(modalTemplate);
        var modalContent = $("#modalContent");
        $(".modal-footer", modalContent).hide();
        var progress = '<div class="progress progress-xs progress-striped active">' +
                '<div style="width: 90%" class="progress-bar progress-bar-success"></div>' +
                '</div>';
        $(".modal-body", modalContent).html(progress + msg);
    }
}

$(function () {
//    $.messager.alert('错误提示', 1, 'error');
//    $.messager.confirm('安装提示', 1111, function (result) {
//        if (result)
//            alert(result);
//    });
});

(function($) {
    jQuery.dialog = function (url,title,data,w,id) {
        if(!w){
            w = '32'
        }
        if(!data){
            //data="t=1";
        }
        if(!id){
            id='dialogPage';
        }
        var dialogPage = dialog({
            id: id,
            "title": title,
            "width": w+'em',
            padding: 0,
            zIndex: 1031
        });
        $.ajax({
            url:url,
            data:data,
            success: function (html) {
                dialogPage.content(html);
                dialogPage.show();
                $('#close-dialog').click(function () {
                    dialogPage.close().remove();
                });
            }
        });
    }
})(jQuery);

(function(jQuery){

    if(jQuery.browser) return;

    jQuery.browser = {};
    jQuery.browser.mozilla = false;
    jQuery.browser.webkit = false;
    jQuery.browser.opera = false;
    jQuery.browser.msie = false;

    var nAgt = navigator.userAgent;
    jQuery.browser.name = navigator.appName;
    jQuery.browser.fullVersion = ''+parseFloat(navigator.appVersion);
    jQuery.browser.majorVersion = parseInt(navigator.appVersion,10);
    var nameOffset,verOffset,ix;

    // In Opera, the true version is after "Opera" or after "Version"
    if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
        jQuery.browser.opera = true;
        jQuery.browser.name = "Opera";
        jQuery.browser.fullVersion = nAgt.substring(verOffset+6);
        if ((verOffset=nAgt.indexOf("Version"))!=-1)
            jQuery.browser.fullVersion = nAgt.substring(verOffset+8);
    }
    // In MSIE, the true version is after "MSIE" in userAgent
    else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
        jQuery.browser.msie = true;
        jQuery.browser.name = "Microsoft Internet Explorer";
        jQuery.browser.fullVersion = nAgt.substring(verOffset+5);
    }
    // In Chrome, the true version is after "Chrome"
    else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
        jQuery.browser.webkit = true;
        jQuery.browser.name = "Chrome";
        jQuery.browser.fullVersion = nAgt.substring(verOffset+7);
    }
    // In Safari, the true version is after "Safari" or after "Version"
    else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
        jQuery.browser.webkit = true;
        jQuery.browser.name = "Safari";
        jQuery.browser.fullVersion = nAgt.substring(verOffset+7);
        if ((verOffset=nAgt.indexOf("Version"))!=-1)
            jQuery.browser.fullVersion = nAgt.substring(verOffset+8);
    }
    // In Firefox, the true version is after "Firefox"
    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
        jQuery.browser.mozilla = true;
        jQuery.browser.name = "Firefox";
        jQuery.browser.fullVersion = nAgt.substring(verOffset+8);
    }
    // In most other browsers, "name/version" is at the end of userAgent
    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
        (verOffset=nAgt.lastIndexOf('/')) )
    {
        jQuery.browser.name = nAgt.substring(nameOffset,verOffset);
        jQuery.browser.fullVersion = nAgt.substring(verOffset+1);
        if (jQuery.browser.name.toLowerCase()==jQuery.browser.name.toUpperCase()) {
            jQuery.browser.name = navigator.appName;
        }
    }
    // trim the fullVersion string at semicolon/space if present
    if ((ix=jQuery.browser.fullVersion.indexOf(";"))!=-1)
        jQuery.browser.fullVersion=jQuery.browser.fullVersion.substring(0,ix);
    if ((ix=jQuery.browser.fullVersion.indexOf(" "))!=-1)
        jQuery.browser.fullVersion=jQuery.browser.fullVersion.substring(0,ix);

    jQuery.browser.majorVersion = parseInt(''+jQuery.browser.fullVersion,10);
    if (isNaN(jQuery.browser.majorVersion)) {
        jQuery.browser.fullVersion = ''+parseFloat(navigator.appVersion);
        jQuery.browser.majorVersion = parseInt(navigator.appVersion,10);
    }
    jQuery.browser.version = jQuery.browser.majorVersion;
})(jQuery);


//缩略图上传初始化
function thumbUpload(id) {
    $("#"+id).click();
    return false;
}


function fileupload(action,obj, input, img,base,thumb) {
    obj.fileupload({
        url: action,
        dataType: 'json',
        done: function (e, data) {
            if(data.result.err!=null){
                layer.msg(data.result.err, {time: 2000});
                return;
            }
            input.val(data.result.msg.url);
            if(!base){
                base = "";
            }
            if(thumb){
                img.attr("src", base+data.result.msg.urla);
            }else{
                img.attr("src", base+data.result.msg.url);
            }
            img.show();
            layer.msg('上传成功', {time: 2000});
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            if($('.layui-layer-content').length>0){
                $('.layui-layer-content').html('上传中(' + progress + '%)');
            }else{
                layer.msg('上传中', {time: 20000});
            }
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
}

function fileuploadmore(action,obj,content,name,base) {
    obj.fileupload({
        url: action,
        dataType: 'json',
        done: function (e, data) {
            if(data.result.err!=null){
                layer.msg(data.result.err, {time: 2000});
                return;
            }
            if(!base){
                base = "";
            }
            var id = "u"+parseInt(Math.random()*10000);
            var html = ' <div id="upload_img_'+id+'"  style="float: left;width: 110px;text-align: center">'+
                '<img src="'+base+data.result.msg.url+'"'+
            'style="width:100px;height:100px;display:block;border:1px solid #ddd;padding:2px;float: left;margin: 5px;"/>'+
            '<input type="hidden" name="'+name+'"  value="'+data.result.msg.url+'"/>'+
            '<a href="javascript:delImg(\''+id+'\')">删除</a>'+
            '</div>';
            content.append(html);
            //input.val(data.result.msg.url);
            //img.attr("src", base+data.result.msg.url);
            //img.show();
            layer.msg('上传成功', {time: 2000});
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            if($('.layui-layer-content').length>0){
                $('.layui-layer-content').html('上传中(' + progress + '%)');
            }else{
                layer.msg('上传中', {time: 20000});
            }
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
}

function delImg(id){
    $("#upload_img_"+id).remove();
}

//数字选择控件
function numberC(id,step,min,max){
    var val = $("#"+id).val();
    if(step>0&&val>=max){
        $("#star").val(max);
        return;
    }
    if(step<0&&val<=min){
        $("#"+id).val(min);
        return;
    }
    val = parseFloat(val);
    val = val + step;
    if((step<1&&step>0)||(step>-1&&step<0)){
        val = val.toFixed(1);
    }

    $("#"+id).val(val);
}

function showImg(url){
    var dialogPage = dialog({
        id: 'dialogPage',
        "title": '查看图片',
        "width":'50em',
        padding: 0,
        zIndex: 1031
    });
    dialogPage.content('<div style="margin: 20px auto;width: 100%;text-align: center"><img src="'+url+'" width="500"/></div>');
    dialogPage.show();

}